package com.jaya.budan.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.amap.api.services.core.PoiItem;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.jaya.budan.R;
import com.jaya.budan.bean.User_Limit;
import com.jaya.budan.views.Circle_ImageView;

import java.util.ArrayList;
import java.util.List;

public class HorizontalListViewAdapter extends BaseAdapter {

    private Context mcontext;
    private int screenWidth;
    private ArrayList<User_Limit> mlist;
    private List<PoiItem> list;
    private int mtype;
    public HorizontalListViewAdapter(Context context, int width, ArrayList<User_Limit> userLimits, List<PoiItem> poiItems, int type){
        mcontext = context;
        screenWidth = width;
        mlist = userLimits;
        list = poiItems;
        mtype = type;
    }

    @Override
    public int getCount() {
        if (mtype == 0){
            return mlist.size();
        } else {
            return list.size();
        }
    }

    @Override
    public Object getItem(int position) {
        if (mtype == 0){
            return mlist.get(position);
        } else {
            return list.get(position);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (mtype == 0){
            ViewHolder holder;
            if (convertView == null){
                holder = new ViewHolder();
                convertView = View.inflate(mcontext, R.layout.main_listview_item, null);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.ll_main_item = convertView.findViewById(R.id.ll_main_item);
            holder.tv_main_lv_item = convertView.findViewById(R.id.tv_main_lv_item);
            holder.iv_main_item = convertView.findViewById(R.id.iv_main_item);
            //计算每条的宽度
            ViewGroup.LayoutParams layoutParams = holder.ll_main_item.getLayoutParams();
            layoutParams.width = screenWidth / 7 * 2;
            holder.ll_main_item.setLayoutParams(layoutParams);
            holder.tv_main_lv_item.setText("" + mlist.get(position).getNicename());
            Glide.with(mcontext).load(mlist.get(position).getHead_path()).apply(new RequestOptions()
                    .placeholder(R.mipmap.iv_main_item).error(R.mipmap.iv_main_item)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)//关闭Glide的硬盘缓存机制
            ).into(holder.iv_main_item);
        } else {
            convertView = View.inflate(mcontext, R.layout.publish_listview_item, null);
            TextView tv_main_lv_item = convertView.findViewById(R.id.tv_main_lv_item);
            TextView tv_main_lv_item_xq = convertView.findViewById(R.id.tv_main_lv_item_xq);
            tv_main_lv_item.setText("" + list.get(position).getTitle());
            tv_main_lv_item_xq.setText("" + list.get(position).getProvinceName() + list.get(position).getCityName() +
                    list.get(position).getAdName() + list.get(position).getSnippet());
        }
        return convertView;
    }

    public class ViewHolder {
        LinearLayout ll_main_item;
        TextView tv_main_lv_item;
        Circle_ImageView iv_main_item;
    }
}
